/**
 * 获取随机数据
 * @param times
 * @returns {Array}
 */
function getRandom1h2h2(times) {
    var randoms = [];
    for (var i = 0; i < times; i++) {
        randoms[randoms.length] = Mock.mock({
            "number|100-200.2": 1
        }).number;
    }
    return randoms;
}

function getRandom30h40h2(times) {
    var randoms = [];
    for (var i = 0; i < times; i++) {
        randoms[randoms.length] = Mock.mock({
            "number|3000-4000.2": 1
        }).number;
    }
    return randoms;
}

function getRandom900h1200h2(times) {
    var randoms = [];
    for (var i = 0; i < times; i++) {
        randoms[randoms.length] = Mock.mock({
            "number|9000-12000.2": 1
        }).number;
    }
    return randoms;
}

function getIncrement(increment, min, times) {
    var data = [],
        time = (new Date()).getTime(),
        i = 0 - times + 1;

    for (i; i <= 0; i++) {
        data.push({
            x: time + i * 5000,
            y: min + increment * (i + 19)
        });
    }
    return data;

}
function getarea(areaname, num) {
    var area = eval(areaname);
    var areas = [];
    for (var i = 0; i < num; i++) {
        var one = [];
        one[0] = area[i] ;
        areas[areas.length] = one;
    }
    return areas;
}

/**
 * 排序
 */
function getSort(array) {
    return array.sort(function (a, b) {
        return b - a
    });
}

/**
 * Created by Administrator on 2016/3/24 0024.
 * 时间初始化及快捷选择
 */
function datepickerInit(id) {
    $('#' + id).datepicker({
        format: "yyyy-mm-dd",
        startDate: "2014-01-01",
        endDate: "2050-01-01",
        maxViewMode: 0,
        todayBtn: "linked",
        language: "zh-CN",
        orientation: "bottom left",
        autoclose: true
    });
}

function checkdate(date) {
    if (date > 9) {
        return date;
    } else {
        return '0' + date;
    }
}

function getlastmonth() {
    var now = new Date();
    var month = now.getMonth() + 1;
    if (month == 1) {
        now.setMonth(12);
    } else {
        now.setMonth(month - 1);
    }
    var day = new Date(now.getFullYear(), now.getMonth(), 0);
    var endDate = now.getFullYear() + '-' + checkdate(now.getMonth()) + '-' + day.getDate();
    var startDate = now.getFullYear() + '-' + checkdate(now.getMonth()) + '-01';
    $("#startDate").val(startDate);
    $("#endDate").val(endDate);
}

function getlastweek() {
    var now = new Date();
    var date = now.getDate();
    var week = now.getDay();
    if (week == 0) {
        week = 7;
    }
    now.setDate(date - week);
    var realEndMonth = now.getMonth() + 1;
    var endDate = now.getFullYear() + '-' + checkdate(realEndMonth) + '-' + checkdate(now.getDate());
    now.setDate(now.getDate() - 6);
    var realStartMonth = now.getMonth() + 1;
    var startDate = now.getFullYear() + '-' + checkdate(realStartMonth) + '-' + checkdate(now.getDate());
    $("#startDate").val(startDate);
    $("#endDate").val(endDate);
}

function getlastday() {
    var now = new Date();
    var date = now.getDate();
    now.setDate(date - 1);
    var realEndMonth = now.getMonth() + 1;
    var startDate = now.getFullYear() + '-' + checkdate(realEndMonth) + '-' + checkdate(now.getDate());
    $("#startDate").val(startDate);
    $("#endDate").val(startDate);
}

function getlastyear() {
    var now = new Date();
    var year = now.getFullYear();
    var lastyear = year - 1;
    $("#startDate").val(lastyear + '-01-01');
    $("#endDate").val(lastyear + '-12-31');
}

$(function () {
    //默认时间
    var today = new Date();
    var yestoday = today.getDate() - 1;
    today.setDate(yestoday);
    var thismonth = today.getMonth() + 1;
    $("#endDate").val(today.getFullYear() + '-' + checkdate(thismonth) + '-' + checkdate(today.getDate()));
    var sevenago = today.getDate() - 6;
    today.setDate(sevenago);
    var sevenagomonth = today.getMonth() + 1;
    $("#startDate").val(today.getFullYear() + '-01-01');

    datepickerInit('startDiv');
    datepickerInit('endDiv');

    //客户名称列表
    var select = document.getElementById("storeName");
    if (select) {
        $.each(store_name, function (item, element) {
            select.options[select.length] = new Option(element[0], element[0]);
        })

    }
});

/**
 *  得到月份差
 */
function getSpace() {
    var startDate = new Date($('#startDate').val());
    var startMonth = startDate.getMonth();
    var endDate = new Date($('#endDate').val());
    var endMonth = endDate.getMonth();
    return endMonth - startMonth + 1;
}

function getMonth(space) {
    var startDate = new Date($('#startDate').val());
    var startMonth = startDate.getMonth();
    var xAxis = [];
    for (var i = 0; i < space; i++) {
        xAxis[xAxis.length] = startMonth + i + 1 + '月';
    }
    return xAxis;
}

function getDate(space) {
    var startDate = new Date($('#startDate').val());
    var endDate = new Date($('#endDate').val());
    var start = startDate.getDate();
    var end = endDate.getDate();
    var xAxis = [];
    if (space == 1) {

        for (var i = 0; i < end - start + 1; i++) {
            xAxis[xAxis.length] = start + i;
        }
    } else if (space == 2) {
        var month = startDate.getMonth() + 1;
        if (month == 1) {
            startDate.setMonth(12);
        } else {
            startDate.setMonth(month - 1);
        }
        var day = new Date(startDate.getFullYear(), startDate.getMonth(), 0);
        var monthEnd = day.getDate();
        for (var i = 0; i <= monthEnd - start; i++) {
            xAxis[xAxis.length] = start + i;
        }
        for (var j = 0; j < end - 1; j++) {
            xAxis[xAxis.length] = 1 + j;
        }
    }

    return xAxis;
}